local execute = vim.api.nvim_command
local fn = vim.fn

local pack_path = fn.stdpath('data') .. '/site/pack'

local install_path = pack_path .. '/packer/start/packer.nvim'

if fn.empty(fn.glob(install_path)) > 0 then
    fn.system({
        'git', 'clone', 'https://github.com/wbthomason/packer.nvim',
        install_path
    })
    execute 'packadd packer.nvim'
end

local packer = require('packer')

local use = packer.use

packer.init({
    ensure_dependencies = true
    -- compile_path = pack_path..'/config/compile.lua'
});

return packer.startup(function()
    -- Packer can manage itself
    use {'wbthomason/packer.nvim'}

    -- LSP
    use {
        'neovim/nvim-lspconfig',
        config = function() require('plugins.lsp_config') end
    }

    use {'nvim-lua/lsp-status.nvim', before = {'kabouzeid/nvim-lspinstall'}}

    use {
        'kabouzeid/nvim-lspinstall',
        event = 'VimEnter',
        config = function() require('plugins.lsp_install') end
    }

    use {
        'ray-x/lsp_signature.nvim',
        before = {'kabouzeid/nvim-lspinstall'},
        config = function() require('plugins.lsp_signature') end
    }

    use {
        'onsails/lspkind-nvim',
        event = 'BufEnter',
        config = function() require('plugins.lsp_kind') end
    }

    use {
        'folke/trouble.nvim',
        requires = 'kyazdani42/nvim-web-devicons',
        cmd = {'Trouble', 'TroubleToggle'},
        config = function() require('plugins.lsp_trouble') end
    }

    -- use {
    --     'glepnir/lspsaga.nvim',
    --     cmd = 'Lspsaga',
    --     config = function() require('plugins.lsp_saga') end
    -- }

    -- Neoformat
    use {
        'sbdchd/neoformat',
        cmd = 'Neoformat',
        config = function() require 'plugins.neoformat' end,
        event = 'BufRead'
    }

    -- Icons
    use {'kyazdani42/nvim-web-devicons'}

    -- Status Line and Bufferline
    use {
        'glepnir/galaxyline.nvim',
        after = 'nvim-web-devicons',
        config = function() require('plugins.ui_galaxyline') end,
        requires = {'Iron-E/nvim-highlite'}
    }
    use {
        'akinsho/nvim-bufferline.lua',
        after = 'galaxyline.nvim',
        config = function() require('plugins.ui_bufferline') end
    }

    use {'itchyny/vim-cursorword'}

    -- File tree
    use {
        'kyazdani42/nvim-tree.lua',
        cmd = {'NvimTreeToggle', 'NvimTreeOpen', 'NvimTreeFindFile'},
        config = function() require('plugins.nvim_tree') end
    }

    -- Git
    use {
        'lewis6991/gitsigns.nvim',
        requires = {'nvim-lua/plenary.nvim'},
        config = function() require('plugins.git_signs') end
    }

    use {'tpope/vim-fugitive'}

    use {
        'kdheepak/lazygit.nvim',
        cmd = 'LazyGit',
        config = function() require('plugins.lazygit') end
    }

    use {
        'nvim-telescope/telescope.nvim',
        cmd = 'Telescope',
        requires = {{'nvim-lua/popup.nvim'}, {'nvim-lua/plenary.nvim'}},
        config = function() require 'plugins.telescope' end
    }

    use {'nvim-telescope/telescope-fzf-native.nvim', run = 'make'}
    use {'nvim-telescope/telescope-media-files.nvim'}

    -- Complete
    use {
        'hrsh7th/vim-vsnip',
        event = 'InsertEnter',
        config = function() require('plugins.vsnip') end
    }
    use {'rafamadriz/friendly-snippets', event = 'InsertCharPre'}

    use {
        'hrsh7th/nvim-compe',
        event = 'InsertEnter',
        after = 'nvim-lspconfig',
        config = function() require('plugins.lsp_compe') end
    }

    -- Treesitter
    use {
        'nvim-treesitter/nvim-treesitter',
        event = 'BufRead',
        config = function() require('plugins.treesitter') end
    }
    use {
        'nvim-treesitter/nvim-treesitter-textobjects',
        after = 'nvim-treesitter',
        disable = false
    }

    use {
        'andymass/vim-matchup',
        event = 'CursorMoved',
        config = function() require('plugins.ui_matchup') end,
        disable = false
    }


    use {
        'windwp/nvim-autopairs',
        after = {'nvim-compe'},
        config = function() require('plugins.autopairs') end
    }

    use {'tpope/vim-surround'}
    use {'tpope/vim-repeat'}

    -- Theme
    -- use {
    --     'sainnhe/gruvbox-material',
    --     config = function() require('plugins.theme_gruvbox') end
    -- }

    -- use {
    --     'glepnir/zephyr-nvim',
    --     config = function() require('plugins.theme_zephyr') end
    -- }

    use {
        'folke/tokyonight.nvim',
        config = function() require('plugins.theme_tokyonight') end
    }

    -- use {
    --     'marko-cerovac/material.nvim',
    --     config = function() require('plugins.theme_material') end
    -- }

    -- use {
    --     'projekt0n/github-nvim-theme',
    --     config = function() require('plugins.theme_github') end
    -- }

    -- use {
    --     'lukas-reineke/indent-blankline.nvim',
    --     event = 'BufRead',
    --     setup = function() require('plugins.indentline') end
    -- }

    -- Dashboard
    use {
        'glepnir/dashboard-nvim',
        setup = function() require('plugins.dashboard') end
    }

    -- UI
    use {
        'norcalli/nvim-colorizer.lua',
        event = 'BufWinEnter',
        config = function() require('plugins.ui_colorizer') end,
        disable = false
    }

    -- Markdown preview
    use {
        'iamcco/markdown-preview.nvim',
        run = 'cd app && npm install',
        ft = 'markdown',
        event = 'BufRead'
    }

    -- Edit
    use {
        'folke/todo-comments.nvim',
        requires = 'nvim-lua/plenary.nvim',
        config = function() require('plugins.todo-comments') end
    }

    use {
        'b3nj5m1n/kommentary',
        config = function() require('plugins.kommentary') end
    }

    -- Navigator
    use {
        'phaazon/hop.nvim',
        as = 'hop',
        cmd = {'HopWord', 'HopPattern', 'HopChar1', 'HopChar2', 'HopLine'},
        config = function() require('plugins.hop') end
    }

    use {
        'numToStr/Navigator.nvim',
        config = function() require('plugins.navigator') end
    }

    use {
        'liuchengxu/vista.vim',
        config = function() require('plugins.vista') end
    }

    -- Moving
    use {"PHSix/faster.nvim", event = 'VimEnter'}

    -- Yank
    use {
        'ojroques/vim-oscyank',
        config = function() require('plugins.oscyank') end
    }
end)
